package com.lwei.tallybook.service.vo.response;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.lwei.tallybook.base.domain.AccountBook;
import com.lwei.tallybook.service.vo.ResponseVO;
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 账本展示实体
 * @author sks.lwei
 */
@Getter
@ToString
public class BookResponseVO implements ResponseVO {

    /**
     * 消费类型
     */
    private String consumeType;
    private String consumeTypeName;
    /**
     * 消费名称
     */
    private String consumeName;
    /**
     * 价格
     */
    private Double consumePrice;
    /**
     * 消费时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date consumeTime;
    /**
     * 消费所有者
     */
    private String consumeOwner;
    private String consumeOwnerName;
    /**
     * 是否关闭
     */
    private Boolean close;
    /**
     * 消费参与者
     */
    private List<String> consumeJoin;
    private List<String> consumeJoinName;

    public BookResponseVO(AccountBook book) {
        this.consumeType = book.getConsumeType();
        this.consumeName = book.getConsumeName();
        this.consumePrice = book.getConsumePrice();
        this.consumeTime = book.getConsumeTime();
        this.consumeOwner = book.getConsumeOwner();
        this.close = book.getClose();
    }

    private BookResponseVO(AccountBook book, @NonNull List<String> consumeJoin, Map<String, Object> param) {
        this.consumeType = book.getConsumeType();
        this.consumeName = book.getConsumeName();
        this.consumePrice = book.getConsumePrice();
        this.consumeTime = book.getConsumeTime();
        this.consumeOwner = book.getConsumeOwner();
        this.close = book.getClose();
        this.consumeJoin = consumeJoin;

        if(param != null && param.size() > 0){
            this.consumeTypeName = param.get(consumeType).toString();
            this.consumeOwnerName = param.get(consumeOwner).toString();
            this.consumeJoinName = consumeJoin.stream().map(s -> param.get(s).toString()).collect(Collectors.toList());
        }
    }

    public static BookResponseVO getInstance(AccountBook book) {
        return new BookResponseVO(book);
    }

    public static BookResponseVO getInstance(AccountBook book, List<String> consumeJoin, Map<String, Object> param) {
        return new BookResponseVO(book, consumeJoin, param);
    }


}

